package com.tkjt.pms.persist.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tkjt.pms.common.entity.TkIndexValueMirror;
import com.tkjt.pms.common.entity.TkIndexWeightMirror;
import com.tkjt.pms.common.utils.EsBeanUtils;
import com.tkjt.pms.modules.common.mapper.TkIndexValueMirrorMapper;
import com.tkjt.pms.modules.manageWeight.mapper.WeightMapper;
import com.tkjt.pms.modules.manageWeight.mapper.WeightMirrorMapper;
import com.tkjt.pms.persist.dao.KpiIndexMirrorDao;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Repository("kpiIndexMirrorDao")
public class KpiIndexMirrorDaoImpl implements KpiIndexMirrorDao {

	@Autowired
	private WeightMirrorMapper weightMirrorMapper;
	@Autowired
	private TkIndexValueMirrorMapper tkIndexValueMirrorMapper;

	@Override
	public void updateWeightMirrorEntity(TkIndexWeightMirror entity) {
		QueryWrapper<TkIndexWeightMirror> weightMirrorQueryWrapper = new QueryWrapper<>();
		weightMirrorQueryWrapper.eq("mirrorCode", entity.getMirrorCode());
		weightMirrorQueryWrapper.eq("indexCode", entity.getIndexCode());
		weightMirrorQueryWrapper.eq("orgCode", entity.getOrgCode());
		//criteria.add(Restrictions.eq("indexValueCode", entity.getIndexValueCode()));
		TkIndexWeightMirror dbEntity = weightMirrorMapper.selectOne(weightMirrorQueryWrapper);
		if (null != dbEntity) {
			EsBeanUtils.copyBeanNotNull2Bean(entity, dbEntity);
			weightMirrorMapper.updateById(dbEntity);
		} else {
			weightMirrorMapper.insert(entity);
		}
	}
	
	public void updateIndexMirrorEntity(TkIndexValueMirror entity) {
		QueryWrapper<TkIndexValueMirror> valueMirrorQueryWrapper = new QueryWrapper<>();
		valueMirrorQueryWrapper.eq("mirrorCode", entity.getMirrorCode());
		valueMirrorQueryWrapper.eq("indexDepart", entity.getIndexDepart());
		valueMirrorQueryWrapper.eq("indexValueCode", entity.getIndexValueCode());
		if (null != entity.getIndexExecutor()) {
			valueMirrorQueryWrapper.eq("indexExecutor", entity.getIndexExecutor());
		}
		TkIndexValueMirror dbEntity = tkIndexValueMirrorMapper.selectOne(valueMirrorQueryWrapper);
		if (null != dbEntity) {
			EsBeanUtils.copyBeanNotNull2Bean(entity, dbEntity);
			tkIndexValueMirrorMapper.updateById(dbEntity);
		} else {
			tkIndexValueMirrorMapper.insert(entity);
		}
	
	}
}
